package com.pinyoumei.app.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.pinyoumei.app.activity.ShoppingCenter.WebViewUrlActivity;


public class CommonUtil {

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context context
     * @param pkgName 应用包名
     * @return true:已安装；false：未安装
     */
    public static boolean isPkgInstalled(Context context, String pkgName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        return packageInfo != null;
    }

    /**
     * 跳转至商品详情
     *
     * @param activity Activity
     * @param url      商品详情
     */
    public static void gotoGoodsDetail(Activity activity, String url) {
        try {
            //测试商品url
            Intent intent = new Intent();
            intent.setAction("Android.intent.action.VIEW");
            Uri uri = Uri.parse(url); // 商品地址
            intent.setData(uri);
            intent.setClassName("com.taobao.taobao", "com.taobao.tao.detail.activity.DetailActivity");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 打开优惠券页面
     * @param activity
     * @param url
     */
    public static void gotoCoupon(Activity activity, String url) {
        try {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.View");
            intent.setClassName("com.taobao.taobao", "com.taobao.browser.BrowserActivity");
            Uri uri = Uri.parse(url);//clickUrl,领券地址
            intent.setData(uri);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    /**
     * 打开店铺主页
     * @param activity
     * @param url
     */
    public static void gotoShop(Activity activity, String url) {
        try {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse(url));
            activity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用第三方浏览器打开
     * @param context
     * @param url 要浏览的资源地址
     */
    public static  void openBrowser(Context context,String url){
//        final Intent intent = new Intent();
//        intent.setAction(Intent.ACTION_VIEW);
//        intent.setData(Uri.parse(url));
//        if (intent.resolveActivity(context.getPackageManager()) != null) {
//            final ComponentName componentName = intent.resolveActivity(context.getPackageManager());
//            context.startActivity(Intent.createChooser(intent, "请选择浏览器"));
//        } else {
//            Toast.makeText(context.getApplicationContext(), "请下载浏览器", Toast.LENGTH_SHORT).show();
//        }
        final Intent intent = new Intent(context, WebViewUrlActivity.class);

        intent.putExtra("order_title", "");
        intent.putExtra("url", url);
        context.startActivity(intent);
    }




    /**
     * 跳转到京东
     * @param url 京东商品链接地址
     */
    public static void toJingDong(Activity activity, String url){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse("openapp.jdmobile://virtual?params=%7B%22sourceValue%22:%220_productDetail_97%22,%22des%22:%22productDetail%22,%22skuId%22:%22"+url+"%22,%22category%22:%22jump%22,%22sourceType%22:%22PCUBE_CHANNEL%22%7D ");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(uri);
        activity.startActivity(intent);
    }

    /**
     * 跳转到平多多
     * @param url
     */
    public static void toPingduoduo(Activity activity,/*String goods_id,String pid,*/ String url){
//        String content = "pinduoduo://com.xunmeng.pinduoduo/duo_coupon_landing.html?goods_id="+goods_id+"&pid="+pid+"&t="+url;
        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
    }
    /**
     * 跳转到唯品会
     * @param url
     */
    public static void toWph(Activity activity, String url){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse(url);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(uri);
        activity.startActivity(intent);
    }


    /**
     * 跳转到浏览器打开
     * @param url
     */
    public static void toLiulan(Context context, String url){
//        Uri uri = Uri.parse(url);
//        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//        intent.addCategory(Intent.CATEGORY_DEFAULT);
//        intent.putExtra("url", url);
//        PackageManager pm = context.getPackageManager();
//        List<ResolveInfo> resolveList = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL);
//        if(resolveList.size() > 0) {
//            String title = "choose application";
//            Intent intentChooser = Intent.createChooser(intent, title);
//            context.startActivity(intentChooser);
//        }else {
//            Toast.makeText(context, "no match activity to start!", Toast.LENGTH_SHORT).show();
//        }


        Uri uri = Uri.parse(url);
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setData(uri);
        context.startActivity(intent);


    }




}